package com.xsoft.web.action.monitor;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.xsoft.framework.util.Page;
import com.xsoft.logic.model.monitor.MonitorServer;
import com.xsoft.logic.model.monitor.MonitorServerRef;
import com.xsoft.logic.model.monitor.MonitorServerShell;
import com.xsoft.logic.service.interfaces.monitor.MonitorServerService;
import com.xsoft.web.action.AdminAction;

public class MonitorServerAction  extends AdminAction {
	
	private MonitorServerService monitorServerService;
	
	private MonitorServer vo = new MonitorServer();
	private List<MonitorServer> monitorServerList;
	private List<MonitorServerShell> functionShellList;
	
	private List<MonitorServerRef> monSysRefList;
	private List<MonitorServer> monServerList;
	private final static String __SYSTEM_TYPE = "1";
	private final static String __SERVER_TYPE = "0";
	
	private File myFile; //上传文件用
	private String contentType; //上传文件用
    private String fileName; //上传文件用

	public void setMyFileFileName(String fileName) {
        this.fileName = fileName;
   } 
	
    public void setMyFileContentType(String contentType) {
        this.contentType = contentType;
   }  
    
    
	public String select(){
		Page page = monitorServerService.getDateByPage(gotoPage, pageSize, vo);
		monitorServerList = (List<MonitorServer>)page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	public String selectServerById(){
		MonitorServer vo = (MonitorServer)monitorServerService.load(Long.parseLong(this.request.getParameter("id")));
		createCommonResult(0,"ok",vo);
		return SUCCESS;
	}
	
	public String insert(){
		
		Long ___id = (Long)monitorServerService.insert(vo);
		if(vo.getType().equals(__SYSTEM_TYPE)){
			MonitorServerRef monRef = new MonitorServerRef();
			monRef.setPId("0");
			monRef.setR_id(___id);
			monRef.setT_id(___id+"");
			monRef.setType(__SYSTEM_TYPE);
			monitorServerService.insertMonRef(monRef);
		}
		
		createCommonResult(0,"ok",null);
		
		return SUCCESS;
	}
	
	public String update(){
		
		monitorServerService.update(vo);
		
		createCommonResult(0,"ok",null);
		
		return SUCCESS;
	}
	
	public String delete(){
		monitorServerService.delete(Long.parseLong(request.getParameter("id")));
		monitorServerService.deleteMonServerRef(Long.parseLong(request.getParameter("id")));
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	
	
	public String selectMonSysRef(){
		monSysRefList = monitorServerService.selectMonSysByType();
		createCommonResult(0,"ok",monSysRefList);
		return SUCCESS;
	}
	
	
	public String selectMonServerByType(){
		monServerList = monitorServerService.selectMonServerByType("0");
		createCommonResult(0,"ok",monServerList);
		return SUCCESS;
	}
	
	
	public String insertMonRef(){
		
		String r_ids = StringUtils.trimToEmpty(this.request.getParameter("r_ids")); 
		String t_ids = StringUtils.trimToEmpty(this.request.getParameter("t_ids"));
		String pIds = StringUtils.trimToEmpty(this.request.getParameter("pIds"));
		
		//先清空历史关系
		monitorServerService.deleteMonRefExceptSys();
		
		if(r_ids.length()>0){
			String[] r_idArray = r_ids.split(",");
			String[] t_idArray = t_ids.split(",");
			String[] pIdArray = pIds.split(",");
			try{
				for(int i=0;i<r_idArray.length;i++){
					MonitorServerRef vo1 = new MonitorServerRef();
					vo1.setR_id(Long.parseLong(r_idArray[i]));
					vo1.setT_id(t_idArray[i]);
					vo1.setPId(pIdArray[i]);
					vo1.setType("0");
					monitorServerService.insertMonRef(vo1);
				}
			}catch(Exception e){
				e.printStackTrace();
				createErrorResult("error");
				return SUCCESS;
			}
		}
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	public String selectFunctionShell(){
		MonitorServerShell shell = new MonitorServerShell();
		try{
			shell.setServer_id(Integer.parseInt(request.getParameter("_server_id")));
		}catch(Exception e){
			shell.setServer_id(null);
		}
		
		Page page = monitorServerService.getDateByPage("selectMonitorServerShell",gotoPage, pageSize,shell);
		functionShellList = (List<MonitorServerShell>)page.getResult();
		this.setPageInfo(page);
		return SUCCESS;
	}
	
	
	
	public String uploadShell(){
		
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		MonitorServerShell shell = new MonitorServerShell();
		shell.setShell_function_name(request.getParameter("shell_function_name"));
		shell.setServer_id(Integer.parseInt(request.getParameter("server_id")));
		shell.setServer_name(request.getParameter("server_name"));
		shell.setShell_name(shell.getServer_id()+"_"+shell.getServer_name()+"_"+fileName);
		shell.setShell_paras(request.getParameter("shell_paras"));
		shell.setShell_out_put(request.getParameter("shell_out_put"));
		shell.setWarn_types(request.getParameter("warn_types"));
		
		File savefile = new File(new File(path), shell.getShell_name());
		if (!savefile.getParentFile().exists())
			savefile.getParentFile().mkdirs();
		try {
			monitorServerService.insertMonitorServerShell(shell);
			FileUtils.copyFile(myFile, savefile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	public String updateShell(){
		MonitorServerShell shell = new MonitorServerShell();
		shell.setShell_function_name(request.getParameter("shell_function_name"));
		shell.setId(Long.parseLong(request.getParameter("shell_id")));
		shell.setShell_paras(request.getParameter("shell_paras"));
		shell.setShell_out_put(request.getParameter("shell_out_put"));
		shell.setWarn_types(request.getParameter("warn_types"));
		monitorServerService.updateMonitorServerShell(shell);
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	public String deleteMonitorServerShell(){
		monitorServerService.deleteMonitorServerShell(Long.parseLong(request.getParameter("id")));
		createCommonResult(0,"ok",null);
		return SUCCESS;
	}
	
	
	
	
	
	public MonitorServerService getMonitorServerService() {
		return monitorServerService;
	}
	public void setMonitorServerService(MonitorServerService monitorServerService) {
		this.monitorServerService = monitorServerService;
	}
	public MonitorServer getVo() {
		return vo;
	}
	public void setVo(MonitorServer vo) {
		this.vo = vo;
	}
	public List<MonitorServer> getMonitorServerList() {
		return monitorServerList;
	}
	public void setMonitorServerList(List<MonitorServer> monitorServerList) {
		this.monitorServerList = monitorServerList;
	}


	public List<MonitorServerRef> getMonSysRefList() {
		return monSysRefList;
	}


	public void setMonSysRefList(List<MonitorServerRef> monSysRefList) {
		this.monSysRefList = monSysRefList;
	}


	public List<MonitorServer> getMonServerList() {
		return monServerList;
	}


	public void setMonServerList(List<MonitorServer> monServerList) {
		this.monServerList = monServerList;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public List<MonitorServerShell> getFunctionShellList() {
		return functionShellList;
	}

	public void setFunctionShellList(List<MonitorServerShell> functionShellList) {
		this.functionShellList = functionShellList;
	}

	
	

}
